package cn.edu.xmu.oomall.payment.dao;

import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.payment.mapper.ErrorAccountPoMapper;
import cn.edu.xmu.oomall.payment.mapper.PaymentPoMapper;
import cn.edu.xmu.oomall.payment.mapper.RefundPoMapper;
import cn.edu.xmu.oomall.payment.model.bo.ErrorAccount;
import cn.edu.xmu.oomall.payment.model.bo.Payment;
import cn.edu.xmu.oomall.payment.model.bo.Refund;
import cn.edu.xmu.oomall.payment.model.po.*;
import cn.edu.xmu.oomall.payment.model.vo.ShopErrorAccountRetVo;
import cn.edu.xmu.oomall.payment.model.vo.ShopPaymentRetVo;
import cn.edu.xmu.oomall.payment.model.vo.ShopRefundRetVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;

import static cn.edu.xmu.privilegegateway.annotation.util.Common.cloneVo;

/**
 * @author 张浩山
 */
@Repository
public class ShopPaymentDao {
    @Autowired
    private PaymentPoMapper paymentPoMapper;

    @Autowired
    private RefundPoMapper refundPoMapper;

    @Autowired
    private ErrorAccountPoMapper errorAccountPoMapper;

    public ReturnObject selectPayment(Integer page, Integer pageSize, String documentId, Byte state, ZonedDateTime beginTime, ZonedDateTime endTime) {
        List<PaymentPo> paymentPoList;
        PaymentPoExample example = new PaymentPoExample();
        try {
            PaymentPoExample.Criteria criteria = example.createCriteria();
            if (documentId != null) {
                criteria.andDocumentIdEqualTo(documentId);
            }
            if (state != null) {
                criteria.andStateEqualTo(state);
            }
            if (beginTime != null) {
                criteria.andGmtCreateGreaterThan(beginTime.withZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime());
            }
            if (endTime != null) {
                criteria.andGmtCreateLessThan(endTime.withZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime());
            }
            PageHelper.startPage(page, pageSize);
            paymentPoList = paymentPoMapper.selectByExample(example);
        } catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
        ReturnObject<PageInfo<Object>> ret = new ReturnObject(new PageInfo<>(paymentPoList));
        return Common.getPageRetVo(ret, ShopPaymentRetVo.class);
    }

    public ReturnObject selectPaymentDetail(Long id) {
        PaymentPo paymentPo;
        try {
            paymentPo = paymentPoMapper.selectByPrimaryKey(id);
        } catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
        Payment payment = cloneVo(paymentPo, Payment.class);
        return new ReturnObject<>(payment);
    }

    public ReturnObject updatePayment(Long id, Payment payment) {
        PaymentPo paymentPo = cloneVo(payment, PaymentPo.class);
        int ret;
        try {
            ret = paymentPoMapper.updateByPrimaryKeySelective(paymentPo);
        } catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
        if (ret == 0) {
            return new ReturnObject(ReturnNo.FIELD_NOTVALID);
        } else {
            return new ReturnObject();
        }
    }

    public ReturnObject selectRefund(Integer page, Integer pageSize, String documentId, Byte state, ZonedDateTime beginTime, ZonedDateTime endTime) {
        List<RefundPo> refundPoList;
        RefundPoExample example = new RefundPoExample();
        try {
            RefundPoExample.Criteria criteria = example.createCriteria();
            if (documentId != null) {
                criteria.andDocumentIdEqualTo(documentId);
            }
            if (state != null) {
                criteria.andStateEqualTo(state);
            }
            if (beginTime != null) {
                criteria.andGmtCreateGreaterThan(beginTime.withZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime());
            }
            if (endTime != null) {
                criteria.andGmtCreateLessThan(endTime.withZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime());
            }
            PageHelper.startPage(page, pageSize);
            refundPoList = refundPoMapper.selectByExample(example);
        } catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
        ReturnObject<PageInfo<Object>> ret = new ReturnObject(new PageInfo<>(refundPoList));
        return Common.getPageRetVo(ret, ShopRefundRetVo.class);
    }

    public ReturnObject selectRefundDetail(Long id) {
        RefundPo refundPo;
        try {
            refundPo = refundPoMapper.selectByPrimaryKey(id);
        } catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
        Refund refund = cloneVo(refundPo, Refund.class);
        return new ReturnObject<>(refund);
    }

    public ReturnObject updateRefund(Long id, Refund refund) {
        RefundPo refundPo = cloneVo(refund, RefundPo.class);
        int ret;
        try {
            ret = refundPoMapper.updateByPrimaryKeySelective(refundPo);
        } catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
        if (ret == 0) {
            return new ReturnObject(ReturnNo.FIELD_NOTVALID);
        } else {
            return new ReturnObject();
        }
    }

    public ReturnObject selectErrorAccount(Integer page, Integer pageSize, String documentId, Byte state, ZonedDateTime beginTime, ZonedDateTime endTime) {
        List<ErrorAccountPo> errorAccountPoList;
        ErrorAccountPoExample example = new ErrorAccountPoExample();
        try {
            ErrorAccountPoExample.Criteria criteria = example.createCriteria();
            if (documentId != null) {
                criteria.andDocumentIdEqualTo(documentId);
            }
            if (state != null) {
                criteria.andStateEqualTo(state);
            }
            if (beginTime != null) {
                criteria.andGmtCreateGreaterThan(beginTime.withZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime());
            }
            if (endTime != null) {
                criteria.andGmtCreateLessThan(endTime.withZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime());
            }
            PageHelper.startPage(page, pageSize);
            errorAccountPoList = errorAccountPoMapper.selectByExample(example);
        } catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
        ReturnObject<PageInfo<Object>> ret = new ReturnObject(new PageInfo<>(errorAccountPoList));
        return Common.getPageRetVo(ret, ShopErrorAccountRetVo.class);
    }

    public ReturnObject selectErrorAccountDetail(Long id) {
        ErrorAccountPo errorAccountPo;
        try {
            errorAccountPo = errorAccountPoMapper.selectByPrimaryKey(id);
        } catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
        ErrorAccount errorAccount = cloneVo(errorAccountPo, ErrorAccount.class);
        return new ReturnObject<>(errorAccount);
    }

    public ReturnObject updateErrorAccount(Long id, ErrorAccount errorAccount) {
        ErrorAccountPo errorAccountPo = cloneVo(errorAccount, ErrorAccountPo.class);
        int ret;
        try {
            ret = errorAccountPoMapper.updateByPrimaryKeySelective(errorAccountPo);
        } catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
        if (ret == 0) {
            return new ReturnObject(ReturnNo.FIELD_NOTVALID);
        } else {
            return new ReturnObject();
        }
    }
}
